package project.euler.honk.problem.to50;

import java.util.LinkedList;
import java.util.List;

final class Problem24 {

	private static final int SIZE = 10;
	private static int current = 0;
	private static final List<Character> chars = new LinkedList<>();

	static {
		for (int i = 0; i < SIZE; i++) {
			chars.add((char) ('0' + i));
		}
	}

	private static void print(int step) {
		System.out.print(chars.remove(step));
	}

	private static int fac(int c) {
		int result = 1;
		for (int i = 2; i <= c; i++)
			result *= i;
		return result;
	}

	public static void solve(int left) {
		// advanced
		if (left <= 0)
			return;
		int x = fac(SIZE - current - 1);
		int tmp = x;
		int c = 0;
		while (x < left) {
			c++;
			x += tmp;
		}
		print(c);
		current++;
		if (c == 0)
			solve(left - x);
		else
			solve(left - (x - tmp));
	}

	public static void main(String[] args) {
		long start = System.currentTimeMillis();
		solve(1000000);
		System.out.println("\nTime: " + (System.currentTimeMillis() - start));
	}
}
